//
//  TimeHelper.m
//  HjxWholesaler
//
//  Created by hjxmall-Design on 16/4/27.
//  Copyright © 2016年 HJX. All rights reserved.
//

#import "TimeHelper.h"

@implementation TimeHelper

+ (NSString *)getCurrentTimeFormat:(NSString *)format
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:format];
    NSString* date = [dateFormatter stringFromDate:[NSDate date]];
    return date;
}

+(NSDate *)getDateWithYMDString:(NSString *)timeStr
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    NSDate*date=[formatter dateFromString:timeStr];
    return date;
}
+(NSDate *)getDateWithYMD_HMSString:(NSString *)timeStr
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate*date=[formatter dateFromString:timeStr];
    return date;
}

+(NSString *)getTimeDayWithYMDString:(NSString *)timeStr;
{
    NSArray * arr = [timeStr componentsSeparatedByString:@"-"];
    if (arr.count == 3) {
        return [NSString stringWithFormat:@"%@",arr[2]];
    }
    return nil;
}

+(NSString *)getTimeV_YMDFromDate:(NSDate *)date
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"yyyy/MM/dd"];
    NSString *dateSMS = [formatter stringFromDate:date];
    return dateSMS;
}
+(NSString *)getTimeH_YMDFromDate:(NSDate *)date
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    NSString *dateSMS = [formatter stringFromDate:date];
    return dateSMS;
}
+(NSString *)getTimeYMD_HMSFromDate:(NSDate *)date
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *dateSMS = [formatter stringFromDate:date];
    return dateSMS;
}
+(NSString *)getTime_YMDFromDate:(NSDate *)date
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"yyMMdd"];
    NSString *dateSMS = [formatter stringFromDate:date];
    return dateSMS;
}
+(NSString *)getTime_YMDHMSSSSFromDate:(NSDate *)date
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"yyMMddHHmmssSSS"];
    NSString *dateSMS = [formatter stringFromDate:date];
    return dateSMS;
}
+(NSString *)getTime_YMDHMSFromDate:(NSDate *)date
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"yyMMddHHmmss"];
    NSString *dateSMS = [formatter stringFromDate:date];
    return dateSMS;
}

+(NSString *)getTimeHHmmFromDate:(NSDate *)date
{
    NSDateFormatter *format=[[NSDateFormatter alloc]init];
    [format setDateFormat:@"HH:mm"];
    return [format stringFromDate:date];
}
+(NSString *)getTimeYearFromDate:(NSDate *)date
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"yyyy"];
    NSString *dateSMS = [formatter stringFromDate:date];
    return dateSMS;
}
+(NSString *)getTimeMonthFromDate:(NSDate *)date
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"MM"];
    NSString *dateSMS = [formatter stringFromDate:date];
    return dateSMS;
}
+(NSString *)getYearMonthFromDate:(NSDate *)date
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"yyyy-MM"];
    NSString *dateSMS = [formatter stringFromDate:date];
    return dateSMS;
}
+(NSString *)getTimeDayFromDate:(NSDate *)date
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"dd"];
    NSString *dateSMS = [formatter stringFromDate:date];
    return dateSMS;
}
+(NSString *)getTimeDayDFromDate:(NSDate *)date
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"d"];
    NSString *dateSMS = [formatter stringFromDate:date];
    return dateSMS;
}
+(NSString *)getWeekdayStringFromDate:(NSDate*)date
{
    NSArray *weekdays = [NSArray arrayWithObjects: [NSNull null], @"周日", @"周一", @"周二", @"周三", @"周四", @"周五", @"周六", nil];
    
    NSCalendar *calendar = [NSCalendar currentCalendar];
    
    NSDateComponents *comp = [calendar components:NSCalendarUnitWeekday fromDate:date];
    return [weekdays objectAtIndex:comp.weekday];
}
+(NSInteger)getWeekdayNumStringFromDate:(NSDate*)date
{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    
    NSDateComponents *comp = [calendar components:NSCalendarUnitWeekday fromDate:date];
    return comp.weekday - 1;
}

+(NSString *)getYesterdayTimeFormat:(NSString *)format{
    NSDate *date = [NSDate date];
    NSDate *lastDay = [NSDate dateWithTimeInterval:-24*60*60 sinceDate:date];//前一天
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:format];
    return [dateFormatter stringFromDate:lastDay];
}

+ (NSString *)changeFormatWithDateString:(NSString *)dateStr{
    NSDate *date = [self getDateWithYMDString:dateStr];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"YYYY年MM月dd日"];
    NSString* newDate = [dateFormatter stringFromDate:date];
    return newDate;
}

+(NSString *)getTimeMonthWithYMDString:(NSString *)timeStr;
{
    NSArray * arr = [timeStr componentsSeparatedByString:@"-"];
    if (arr.count == 3) {
        return [NSString stringWithFormat:@"%@",arr[1]];
    }
    return nil;
}

+ (NSString *)getMonthBeginAndEndWith:(NSString *)dateStr{
    
    NSDateFormatter *format=[[NSDateFormatter alloc] init];
    [format setDateFormat:@"yyyy-MM"];
    NSDate *newDate=[format dateFromString:dateStr];
    double interval = 0;
    NSDate *beginDate = nil;
    NSDate *endDate = nil;
    NSCalendar *calendar = [NSCalendar currentCalendar];
    
    [calendar setFirstWeekday:2];//设定周一为周首日
    BOOL ok = [calendar rangeOfUnit:NSCalendarUnitMonth startDate:&beginDate interval:&interval forDate:newDate];
    //分别修改为 NSDayCalendarUnit NSWeekCalendarUnit NSYearCalendarUnit
    if (ok) {
        endDate = [beginDate dateByAddingTimeInterval:interval-1];
    }else {
        return @"";
    }
    NSDateFormatter *myDateFormatter = [[NSDateFormatter alloc] init];
    [myDateFormatter setDateFormat:@"YYYY年MM月dd日"];
    NSString *beginString = [myDateFormatter stringFromDate:beginDate];
    NSString *endString = [myDateFormatter stringFromDate:endDate];
    NSString *s = [NSString stringWithFormat:@"%@-%@",beginString,endString];
    return s;
}

// 获取上月初一的时间
+ (NSString *)getLastMonthFirstDayTime{
    NSDate  *currentDate = [NSDate date];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *components = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:currentDate];
    
    NSInteger year=[components year];
    NSInteger month=[components month];
    //构造上月1号时间
    month --;
    //获取上月月份 没的说
    if (month == 0) {
        month = 12;
        year--;
    }
    NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
    [dateComponents setYear:year];
    [dateComponents setMonth:month];
    [dateComponents setDay:1];
    //上月1号时间
    NSDate *firstDayOfLastMonth = [calendar dateFromComponents:dateComponents];
    NSString *timeStr = [NSString stringWithFormat:@"%ld", (long)[firstDayOfLastMonth timeIntervalSince1970]];
    
    NSTimeInterval time=[timeStr doubleValue]+28800;//因为时差问题要加8小时 == 28800 sec
    NSDate *detaildate=[NSDate dateWithTimeIntervalSince1970:time];
    NSLog(@"date:%@",[detaildate description]);
    //实例化一个NSDateFormatter对象
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    //设定时间格式,这里可以设置成自己需要的格式
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    
    NSString *currentDateStr = [dateFormatter stringFromDate: detaildate];
    return currentDateStr;
}
//获取当前时间戳
+ (NSString *)getCurrentTimestamp{
    NSDate* date = [NSDate dateWithTimeIntervalSinceNow:0];//获取当前时间0秒后的时间
    NSTimeInterval time=[date timeIntervalSince1970]*1000;// *1000 是精确到毫秒，不乘就是精确到秒
    NSString *timeString = [NSString stringWithFormat:@"%.0f", time];
    return timeString;
}


+ (NSString*)getDownTime:(NSString*)time downDays:(NSInteger)days{
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"UTC"];
    [formatter setTimeZone:timeZone];
    [formatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss"];
    NSDate*date=[formatter dateFromString:time];
    NSTimeInterval timeDate=[date timeIntervalSince1970];
    NSTimeInterval timeCurDate=[[NSDate date] timeIntervalSince1970];
    NSTimeInterval donw = timeCurDate - timeDate;
    NSTimeInterval donwAll = days*24*60*60;
    NSTimeInterval remaining = donwAll-donw;
    if (remaining<=0) {
        return nil;
    }
    NSInteger day = (NSInteger)remaining/(24*60*60);
    NSInteger hour = (remaining-day*(24*60*60))/(60*60);
    NSInteger m = (remaining-day*(24*60*60)-hour*60*60)/60;
    return [NSString stringWithFormat:@"%02ld%@ %02ld%@ %02ld%@",day,XLocalized(@"天"),hour,XLocalized(@"时"),m,XLocalized(@"分")];
}

+(NSString *)getUTCFromTime:(NSString*)time
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"UTC"];
    [formatter setTimeZone:timeZone];
    [formatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSS"];
    NSDate*date=[formatter dateFromString:time];
    return [self getTimeYMD_HMSFromDate:date];
}
@end
